<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/icons.css" />
    <script src="../script/jquery-1.9.1.min.js"></script>
    <script src="../script/bootstrap.min.js"></script>
    <style>
        .table tr:first-child {
            background: url(static/img/tableheader-bg.gif) no-repeat left top;
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 13px;
            font-weight: bold;
        }

        .table {
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
        }

        .table td {
            height: 40px;
            text-align: center;
            border: 1px solid #CBD6DE;
            padding: 0;
            margin: 0;
            vertical-align: middle !important;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            任务单处理
        </div>
    </header>
    <div class="aui-content" style="margin-top:50px;">

        <div class="aui-form">

            <ul class='aui-list aui-media-list ui-selectee' style="position: static;">
                <li class='aui-list-item  ui-selectee' style='color: black !important;background-color: transparent'>
                    <div class='aui-list-item-inner ui-selectee '>
                        <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                            <div class='aui-list-item-label ui-selectee list-tr-title'
                                style="font-size: 16px;font-weight: 600;">
                                模具编码:</div>
                            <div class="aui-list-item-input">
                                <input type="text" id="rsBarcode" disabled="true" />
                            </div>
                        </div>
                        <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                            <div class='aui-list-item-label ui-selectee list-tr-title'
                                style="font-size: 16px;font-weight: 600;">
                                模具名称:</div>
                            <div class="aui-list-item-input">
                                <input type="text" id="rsName" disabled="true" />
                            </div>
                        </div>
                        <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                            <div class='aui-list-item-label ui-selectee list-tr-title'
                                style="font-size: 16px;font-weight: 600;">
                                类型:</div>
                            <div class="aui-list-item-input">
                                <input type="text" id="taskType" disabled="true" />
                            </div>
                        </div>
                        <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                            <div class='aui-list-item-label ui-selectee list-tr-title'
                                style="font-size: 16px;font-weight: 600;">
                                任务结果:</div>
                            <div class="aui-list-item-input">
                                <select id="zxResult" style="font-size: 14px;">
                                    <option value="0">未判定</option>
                                    <option value="1">正常</option>
                                    <option value="2">异常</option>
                                </select>
                            </div>
                        </div>
                        <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                            <div class='aui-list-item-label ui-selectee list-tr-title'
                                style="font-size: 16px;font-weight: 600;">
                                备注:</div>
                            <div class="aui-list-item-input">
                                <input type="text" id="remark" />
                            </div>
                        </div>
                    </div>
        </div>
        </li>
        </ul>
        <ul id="myTab" class="nav nav-tabs" style="background-color: #F4F4F4;font-size: 13px;">
            <li class="active" style="width: 100%;text-align:center;"> <a href="#item" data-toggle="tab">检修项 </a>

            </li>

        </ul>
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade in active" id="item">
                <div style="background-color:white; height: 100%;width: 100%;">
                    <table id="chkItemTable" class="table" style="width:200%;">
                        <thead id="chkItemTableHead">
                        </thead>
                    </table>
                    <div class="aui-content">

                        <div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
                            <p>
                            </p>
                            <div class="aui-btn aui-btn-info" onclick="doSave()"  id="submitButton" style="width: 100%;height: 100%;">
                                <span class="aui-iconfont" style="font-size: 20px;">提交数据</span>

                            </div>
                            <p></p>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>

<script>

    var itemIds = "";
    var itemCodes = "";
    var itemModes = "";
    var itemValues = "";
    var josnList = [];
    var delItemIds = "";
    var str = "";


    var rsBarcode = ""; //模具编码
    var type = ""; //模具维保类型
    var rsName = ""; //模具名称
    var taskCode = ""; //任务单号

    apiready = function () {
        type = api.pageParam.type; //模具维保类型
        rsBarcode = api.pageParam.rsBarcode;
        rsName = api.pageParam.rsName;
        taskCode = api.pageParam.taskCode;
        $("#rsBarcode").val(rsBarcode);
        $("#rsName").val(rsName);
        if ('1' == type) {
            $("#taskType").val("模具点检");
        } else if ('0' == type) {
            $("#taskType").val("模具保养");
        }
        getEquipTaskItemByTask()

    }


    function setEquipTask(taskResult) {
        $("#zxResult").children().remove();
        if (taskResult == 0) {
            var str = "<option value='0' selected='true'>未判定</option><option value='1' >正常</option><option value='2'>异常</option>";
            $("#zxResult").append(str);
        } else if (taskResult == 1) {
            var str = "<option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option>";
            $("#zxResult").append(str);
        } else {
            var str = "<option value='0'>未判定</option><option value='1' >正常</option><option value='2' selected='true'>异常</option>";
            $("#zxResult").append(str);
        }

    }


    function getEquipTaskItemByTask() {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getChkItemByTask_MJ";
        var un = localStorage.getItem('un');

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        params.returnAll = 'false';
        params.timeout = 60;

        var data = {
            values: {
                un: un,
                taskCode: taskCode,
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    setEquipTask(myobj.taskResult)

                    document.getElementById("remark").value=myobj.remark

                    taskItemIds = "";
                    itemIds = "";
                    itemCodes = "";
                    remarks = ""
                    itemModes = "";
                    itemValues = "";

                    josnList = [];
                    var list = myobj.list;
                    var html = "";
                    var html1 = "";
                    html1 += "<tr id='tr1' color: #467aa7;>";
                    html1 += "<td style='width:5%' algin='center'>序号</td>";
                    html1 += "<td algin='center' style='display:none;'>编号</td>";
                    html1 += "<td style='width:65%' algin='center'>检查项目</td>";
                    html1 += "<td style='width:30%' algin='center'>检测值</td>";

                    html1 += "</tr>"

                    $api.html($api.byId("chkItemTableHead"), html1);
                    str = "";
                    // console.log(JSON.stringify(list))
                    if (list != undefined) {
                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var taskItemId = list[i].sysid
                            var itemId = list[i].itemId;
                            var itemCode = list[i].itemCode;
                            var chkMode = list[i].chkMode;
                            var chkResult = list[i].chkResult;
                            var remark = list[i].remark;
                            if (i == 0) {
                                taskItemIds = taskItemId
                                itemIds = itemId;
                                itemCodes = itemCode;
                                remarks = remark;

                            } else {
                                taskItemIds = taskItemIds + ',' + taskItemId
                                itemIds = itemIds + "," + itemId;
                                itemCodes = itemCodes + "," + itemCode;
                                remarks = remarks + "," + remark
                            }
                          

                            if (chkMode == "0") {
                                str += "<tr ><td width='5%' algin='center'>" +
                                    "" + "<input type='checkbox' onclick='setDelItem(\""
                                    + itemId + "\");'></td><td algin='center' style='display:none;'>"
                                    + itemCode + "</td>"
                                str += "<td width='30%' algin='center'>" + remark + "</td><td width='10%' algin='center'><select id='"


                                if (chkResult == "1") {

                                    str += itemId + "'><option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option></select>"

                                } else if (chkResult == "2") {

                                    str += itemId + "'><option value='0'>未判定</option><option value='1'>正常</option><option value='2' selected='true'>异常</option></select></td>"

                                } else if (chkResult == "3") {

                                    str += itemId + "'><option value='0' >未判定</option><option value='1'>正常</option><option value='2'>异常</option><option value='3' selected='true'>不适用</option></select></td>"

                                } else {

                                    str += itemId + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td>"

                                }

                            }
                        }
                    }
                    $("#chkItemTableHead").append(str);

                } else if (myobj.code == '1') {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }




    function setDelItem(id) {
        if (delItemIds == "") {
            delItemIds = id;
        } else {
            if (delItemIds.indexOf(id) > 0) {
                //已存在
                var delArr = delItemIds.split(",");
                delItemIds = "";
                for (i = 0; i < delArr.length; i++) {
                    if (delArr[i] != id) {
                        if (i == 0) {
                            delItemIds = delArr[i];
                        } else {
                            delItemIds = delItemIds + "," + delArr[i];
                        }
                    }

                }
            } else {
                //不存在
                delItemIds = delItemIds + "," + id;
            }
        }
    }



    function doSave(type) {
        console.log(itemIds)
        if (itemIds != "") {
            //点检项表格中有数据
            setItemValues();
            //提交数据到服务端
            doSubmit(type);
        } else {
            alert("无点检项信息，无法提交数据");
        }
    }

    function setItemValues() {
		var itemIdArr = itemIds.split(",");
      
		var itemValue = "";
		var un = localStorage.getItem('un');
		for (i = 0; i < itemIdArr.length; i++) {
			var taskItemIdArr = taskItemIds.split(",");
			var itemCodeArr = itemCodes.split(",");
			var remarkArr = remarks.split(",");
			var checkResult = "";
			var taskResult = document.getElementById("zxResult").value;


			//获取下拉框选中的值
			itemValue = document.getElementById(itemIdArr[i]).value;//选中的值
			checkResult = itemValue;


			if (i == 0) {
				itemValues = itemValue;
			} else {
				itemValues = itemValues + "," + itemValue;
			}
			var iObj = new itemObj(itemIdArr[i], itemCodeArr[i], checkResult, un, taskResult, remarkArr[i], taskItemIdArr[i]);
			josnList.push(iObj);
		}

	}
    function itemObj(chkItemId, chkItemCode,  checkResult, chkPerson, taskResult, remark, taskItemId) {
		this.chkItemGid = chkItemId;
		this.chkItemCode = chkItemCode;
		this.checkResult = checkResult;
		this.taskResult = taskResult;
		this.remark = remark;//检查项目
		this.sysid = taskItemId
	}



    function doSubmit() {
		if (!clickThrottle()) {
			return;
		}

		const submitButton = document.getElementById('submitButton');

		submitButton.style.pointerEvents = "none"
		var types = document.getElementById("zxResult").value;
        var remark = document.getElementById("remark").value;

        //存在执行结果的时候，不能存在未判定的数据
		if (types != '0') {
            let containsCheckResultOne = false;
			josnList.forEach(item => {
				if (item.checkResult === "0") {
					containsCheckResultOne = true;
				}
			});
			//说明存在异常的数据
			if (containsCheckResultOne) {
				api.toast({
					msg: '检测项中存在未判定数据项！',
					duration: 3000
				});
				submitButton.style.pointerEvents = "auto"
				josnList = []
				return;
			}
		}
		api.showProgress();
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/saveTaskItem_MJ";
		var un = localStorage.getItem('un');

		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		params.returnAll = 'false';
		params.timeout = 60;

		api.showProgress({
			title: '保存中...',
		});

		var data = {
			values: {
				un: un,
				taskCode: taskCode,
				rowData: encodeURIComponent(JSON.stringify(josnList)),
                remark:remark,
				type: types,
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				//成功
				if (myobj.code == '0') {

					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: "保存成功",
						duration: 3000
					});
					api.closeWin();

					api.execScript({

						name: api.pageParam.name,
						script: 'search()'

					});

				} else if (myobj.code == '1') {
					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			} else {
				api.hideProgress();
				submitButton.style.pointerEvents = "auto"
				alert(JSON.stringify(err));
			}
		});
	}
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>